The sets module provides classes for constructing and manipulating unordered collections of unique elements but is deprecated since version 2.6 and is replaced by the build-in set
and frozenset
types.
1. sets, set and frozenset
The sets module provides classes for constructing and manipulating unordered collections of unique elements but is deprecated since version 2.6 and is replaced by the build-in set
and frozenset
types.
class set([iterable]) # Return a new set object whose elements are taken from iterable.
class frozenset([iterable]) # Return a new frozenset object whose elements are taken from iterable.
# The elements of a set must be hashable.
# To represent sets of sets, the inner sets must be frozenset objects.
2. Basic operations
(1) Operations for both
Instances of set
and frozenset
provide the following operations:
len(s) # Return the cardinality of set s.
x in s # Test x for membership in s.
x not in s # Test x for non-membership in s.
isdisjoint(other) # Return True if the set has no elements in common with other. Sets are disjoint if and only if their intersection is the empty set.
issubset(other)
set <= other # Test whether every element in the set is in other.
set < other # set <= other and set != other.
issuperset(other)
set >= other # Test whether every element in other is in the set.
set > other # set >= other and set != other.
union(other, ...)
set | other | ... # Return a new set with elements from the set and all others.
intersection(other, ...)
set & other & ... # Return a new set with elements common to the set and all others.
difference(other, ...)
set - other - ... # Return a new set with elements in the set that are not in the others.
symmetric_difference(other)
set ^ other # Return a new set with elements in either the set or other but not both.
copy() # Return a new set with a shallow copy of s.
(2) Operations only for set
The following table lists operations available for set that do not apply to immutable instances of frozenset:
# Update the set, adding elements from all others.
update(*others)
set |= other | ...
# Update the set, keeping only elements found in it and all others.
intersection_update(*others)
set &= other & ...
# Update the set, removing elements found in others.
difference_update(*others)
set -= other | ...
# Update the set, keeping only elements found in either set, but not in both.
symmetric_difference_update(other)
set ^= other
add(elem) # Add element elem to the set.
remove(elem) # Remove element elem from the set. Raises KeyError if elem is not contained in the set.
discard(elem) # Remove element elem from the set if it is present.
pop() # Remove and return an arbitrary element from the set. Raises KeyError if the set is empty.
clear() # Remove all elements from the set.
3. set vs frozenset
The elements of a set must be hashable. To represent sets of sets, the inner sets must be frozenset
objects.
(1) a set of sets
a = frozenset(range(5))
b = frozenset(range(5, 10))
c = frozenset(range(5))
d = set([a, b, c])
(2) intersection of a list of sets
>>> list_sets = [set([1, 2, 3]), set([2, 3, 4]), set([3, 4, 5])]
>>> set.intersection(*list_sets) # Note that set.intersection is different from set().intersectionset([3])
# BTW, convert a list of lists to a list of sets by:
list_sets = [set(row) for row in lists]